package com.example.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.stu.entity.Result;
import com.example.stu.mapper.ManagerMapper;
import com.example.stu.mapper.StudentMapper;
import com.example.stu.pojo.ManagerLogin;
import com.example.stu.pojo.Student;
import com.example.stu.service.LoginVerifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Date: 2021/2/26
 * @Author: 黄先军
 * @Description:
 */
@Service
public class LoginVerifyServiceImpl implements LoginVerifyService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ManagerMapper managerMapper;

    /**
     * 学生登录
     * @param stuID
     * @param stuPwd
     * @return
     */
    @Override
    public Result studentLoginVerify(Integer stuID, String stuPwd) {
        if(StringUtils.isBlank(stuPwd) && stuID == null){
            return new Result(false,"用户名或密码不能为空");
        }
        List<Student> studentList = studentMapper.selectList(new LambdaQueryWrapper<Student>()
                .eq(Student::getStuId, stuID)
                .eq(Student::getStuPwd, stuPwd)
                .select(Student::getStuId, Student::getStuPwd));
       if(studentList.size()>0){
           return new Result(true,null);
       }else {
           return new Result(false,"用户名或密码错误");
       }
    }

    /**
     * 管理员登录
     * @param stuId
     * @param stuPwd
     * @return
     */
    @Override
    public Result manageLoginVerify(String stuId, String stuPwd) {
        if(StringUtils.isBlank(stuPwd) && stuId == null){
            return new Result(false,"用户名或密码不能为空");
        }
        ManagerLogin admin = managerMapper.selectOne(new LambdaQueryWrapper<ManagerLogin>()
                .eq(ManagerLogin::getMUsername, stuId)
                .eq(ManagerLogin::getMPassword, stuPwd)
                .select(ManagerLogin::getMUsername, ManagerLogin::getMPassword));
        if(admin!=null){
            return new Result(true,null);
        }else {
            return new Result(false,"用户名和密码错误");
        }
    }

}
